using System;
using System.Collections;
using System.Threading;
using OracleInternal.Common;

namespace OracleInternal.Network
{
	internal class TNSNames : INamingAdapter
	{
		private static ReaderWriterLockSlim myLock;

		private static Hashtable m_TNSNamesMap;

		internal Hashtable m_TNSNamesMapDyn;

		public string ID => "TNSNames";

		public Hashtable Map
		{
			get
			{
				try
				{
					myLock.EnterReadLock();
					return (Hashtable)m_TNSNamesMap.Clone();
				}
				finally
				{
					myLock.ExitReadLock();
				}
			}
		}

		static TNSNames()
		{
			myLock = new ReaderWriterLockSlim();
			m_TNSNamesMap = new Hashtable(StringComparer.OrdinalIgnoreCase);
			ConfigBaseClass.m_TNSNamesoraloc = ProviderConfig.NewOraFileLoc(OraFiles.TnsNames);
			Trace.Write(OracleTraceLevel.Config, OracleTraceTag.Sqlnet, OracleTraceClassName.CustomConfigFileReader, OracleTraceFuncName.ParseConfigFile, "FilePath : " + (string.IsNullOrEmpty(ConfigBaseClass.m_sqlnetOraLoc) ? "(null)" : ConfigBaseClass.m_sqlnetOraLoc));
			Trace.Write(OracleTraceLevel.Config, OracleTraceTag.Tnsnames, OracleTraceClassName.TNSNames, OracleTraceFuncName.TNSNames, "FilePath : " + (string.IsNullOrEmpty(ConfigBaseClass.m_TNSNamesoraloc) ? "(null)" : ConfigBaseClass.m_TNSNamesoraloc));
			ProviderConfig.NewOraFileParams(OraFiles.TnsNames, ConfigBaseClass.m_TNSNamesoraloc, m_TNSNamesMap);
		}

		internal void SetNewHT(Hashtable HT)
		{
			m_TNSNamesMapDyn = HT;
		}

		public string Resolve(string TNSAlias, out ConnectionOption CO, string InstanceName = null, string ServerIs = null)
		{
			try
			{
				myLock.EnterReadLock();
				object obj = ((m_TNSNamesMapDyn != null) ? m_TNSNamesMapDyn[TNSAlias] : m_TNSNamesMap[TNSAlias]);
				CO = null;
				return obj?.ToString();
			}
			finally
			{
				myLock.ExitReadLock();
			}
		}

		public void Refresh()
		{
			try
			{
				myLock.EnterWriteLock();
				ConfigBaseClass.m_ParseMode = ParseMode.ReParseTnsNames;
				ConfigBaseClass.m_TNSNamesoraloc = ProviderConfig.NewOraFileLoc(OraFiles.TnsNames);
				ProviderConfig.NewOraFileParams(OraFiles.TnsNames, ConfigBaseClass.m_TNSNamesoraloc, m_TNSNamesMap);
			}
			finally
			{
				myLock.ExitWriteLock();
				ConfigBaseClass.m_ParseMode = ParseMode.None;
			}
		}
	}
}
